﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Drawing;
using System.Linq;

namespace TestThumbGen
{
    using System.IO;
    using System.Reflection;

    using ThumbGen;

    using ThumbnailGenerator = ThumbGen.ThumbnailGenerator;

    /// <summary>
    ///This is a test class for ThumbnailGeneratorTest and is intended
    ///to contain all ThumbnailGeneratorTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ThumbnailGeneratorTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test with empty FileName
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(InvalidOperationException))]
        public void EmptyFileNameTest()
        {
            ThumbnailGenerator thumbnailGenerator = new ThumbnailGenerator();
            Bitmap actual = thumbnailGenerator.GetThumbnail(TimeSpan.Zero);
            Assert.IsTrue(true, "EmptyFileNameTest");
        }

        /// <summary>
        ///A test for FileName
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Wildlife.wmv")]
        public void FileNameTest()
        {
            ThumbnailGenerator thumbnailGenerator = new ThumbnailGenerator();
            thumbnailGenerator.FileName = "Wildlife.wmv";
            Assert.IsTrue(true, "FileNameTest");
        }

        /// <summary>
        ///A test for a bad FileName
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Wildlife.wmv")]
        [ExpectedException(typeof(ArgumentException))]
        public void BadFileNameTest()
        {
            ThumbnailGenerator thumbnailGenerator = new ThumbnailGenerator();
            thumbnailGenerator.FileName = "FooFoo.wmv";
            Assert.IsTrue(true, "BadFileNameTest");
        }

        /// <summary>
        ///A test for a bad output directory.
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentException))]
        [DeploymentItem("Wildlife.wmv")]
        public void BadOutputDirectory()
        {
            ThumbnailGenerator thumbnailGenerator = new ThumbnailGenerator();
            thumbnailGenerator.FileName = "Wildlife.wmv";
            thumbnailGenerator.OutputDirectory = "ZZ:\\Output";
        }

        /// <summary>
        ///A test for GetThumbnail
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Wildlife.wmv")]
        public void GetThumbnailTest()
        {
            ThumbnailGenerator target = new ThumbnailGenerator(); 
            target.FileName = "Wildlife.wmv";
            TimeSpan time = TimeSpan.Zero;
            Bitmap actual = target.GetThumbnail(time);
            Assert.AreEqual(320, actual.Width, "Expecting 320 width by default");
            Assert.AreEqual(320 / 16 * 9, actual.Height, "Height not expected value for 16:9 value.");
        }

        /// <summary>
        ///A test for GetThumbnail
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Wildlife.wmv")]
        public void GetThumbnailWidthTest()
        {
            int width = 100;
            ThumbnailGenerator thumbnailGenerator = new ThumbnailGenerator();
            thumbnailGenerator.FileName = "Wildlife.wmv";
            thumbnailGenerator.Width = width;
            TimeSpan time = TimeSpan.Zero;
            Bitmap actual = thumbnailGenerator.GetThumbnail(time);
            Assert.AreEqual(width, actual.Width, "Expecting 100 width");
            Assert.AreEqual(Math.Round(width * 9 / 16.0), actual.Height, "Height not expected value for 16:9 value.");
        }
        /// <summary>
        ///A test for GetThumbnail
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Audio.mp3")]
        [ExpectedException(typeof(ArgumentException))]
        public void TestAudio()
        {
            ThumbnailGenerator target = new ThumbnailGenerator();
            target.FileName = "Audio.mp3";
            TimeSpan time = TimeSpan.Zero;
            Bitmap actual = target.GetThumbnail(time);
            Assert.AreEqual(actual.Width, 320, "Expecting 320 width by default");
            Assert.AreEqual(actual.Height, 320 / 16 * 9, "Height not expected value for 16:9 value.");
        }

        /// <summary>
        /// Save to output directory.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Wildlife.wmv")]
        public void NoOutputDirectoryTest()
        {
            string currentDirectory = Directory.GetCurrentDirectory();
            DirectoryInfo directoryInfo = new DirectoryInfo(currentDirectory);
            int numberOfFiles = directoryInfo.EnumerateFiles().Count();
            
            ThumbnailGenerator thumbnailGenerator = new ThumbnailGenerator("Wildlife.wmv");
            thumbnailGenerator.GenerateThumbnails();

            int numberOfFilesAfter = directoryInfo.EnumerateFiles().Count();

            Assert.AreEqual(numberOfFiles + 7, numberOfFilesAfter, "Unexpected number of thumbnails");
        }
        
        /// <summary>
        /// Save to output directory.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Wildlife.wmv")]
        public void SaveToOutputDirectoryTest()
        {
            string thumbnailPath = @"c:\temp\thumbnails";
            
            DeleteDirectory(thumbnailPath);

            ThumbnailGenerator thumbnailGenerator = new ThumbnailGenerator("Wildlife.wmv");
            thumbnailGenerator.OutputDirectory = @"C:\temp\Thumbnails";
            thumbnailGenerator.TimeInterval = TimeSpan.FromSeconds(5);

            thumbnailGenerator.GenerateThumbnails();
            DirectoryInfo directoryInfo = new DirectoryInfo(thumbnailPath);
            int numberOfThumbnails = directoryInfo.EnumerateFiles().Count();

            Assert.AreEqual(numberOfThumbnails, 7, "Unexpected number of thumbnails");
        }

        [TestMethod()]
        [DeploymentItem("Wildlife.wmv")]
        public void SaveAsPNGWithName()
        {
            string thumbnailPath = @"c:\temp\thumbnails";

            DeleteDirectory(thumbnailPath);

            ThumbnailGenerator thumbnailGenerator = new ThumbnailGenerator("Wildlife.wmv");
            thumbnailGenerator.OutputDirectory = @"C:\temp\Thumbnails";
            thumbnailGenerator.TimeInterval = TimeSpan.FromSeconds(5);
            thumbnailGenerator.Format = Format.Png;
            thumbnailGenerator.Name = "Fred";

            thumbnailGenerator.GenerateThumbnails();
            DirectoryInfo directoryInfo = new DirectoryInfo(thumbnailPath);
            int numberOfThumbnails = directoryInfo.EnumerateFiles("Fred*").Count();

            Assert.AreEqual(numberOfThumbnails, 7, "Unexpected number of thumbnails");
        }

        private void DeleteDirectory(string thumbnailPath)
        {
            try
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(thumbnailPath);
                directoryInfo.Delete(true /* recursive */);
            }
            catch (DirectoryNotFoundException)
            {
                // If it wasn't found then we're OK.
            }
        }
    }
}
